<template>
  <div
    class="shelf-item"
    :class="{ 'shelf-item-shadow': data.type === 1 || data.type === 2 }"
    @click="onItemClick"
  >
    <component
      class="shelf-item-comp"
      :class="{
        'is-edit': isEditMode && data.type === 2,
      }"
      :is="item"
      :data="data"
    ></component>
    <div
      class="icon-selected"
      :class="{ 'is-selected': data.selected }"
      v-show="isEditMode && data.type === 1"
    ></div>
  </div>
</template>

<script>
import { storeShelfMixin } from '../../utils/mixin'
import ShelfBook from './ShelfItemBook'
import ShelfCategory from './ShelfItemCategory'
import ShelfAdd from './ShelfItemAdd'
import { gotoStoreHome } from '../../utils/store'

export default {
  mixins: [storeShelfMixin],
  props: {
    data: Object
  },
  data() {
    return {
      book: ShelfBook,
      category: ShelfCategory,
      add: ShelfAdd
    }
  },
  computed: {
    // 说明列表显示的是哪种类型的
    item() {
      return this.data.type === 1 ? this.book : (this.data.type === 2 ? this.category : this.add)
    }
  },
  methods: {
    // 点击书籍
    onItemClick() {
      // console.log(this.data)
      if (this.isEditMode) {
        // 编辑模式
        if (this.data.type === 1) {
          this.data.selected = !this.data.selected
        }
        if (this.data.selected) {
          // console.log(this.data)
          // 扩展的数组方法 避免重复添加 添加选中书籍到选中数组中
          this.shelfSelected.pushWithoutDuplicate(this.data)
          this.setShelfSelected(this.shelfSelected)
        } else {
          // 不是选中状态 则将该书籍从选中书籍数组中移除
          this.setShelfSelected(this.shelfSelected.filter(item => item.id !== this.data.id))
        }
      } else {
        // 非编辑模式
        if (this.data.type === 1) {
          this.showBookDetail(this.data)
        } else if (this.data.type === 2) {
          this.$router.push({
            path: '/store/category',
            query: {
              title: this.data.title
            }
          })
        } else {
          gotoStoreHome(this)
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import "../../assets/styles/global";

.shelf-item {
  position: relative;
  width: 100%;
  height: 100%;
  // background: red;
  &.shelf-item-shadow {
    box-shadow: px2rem(2) px2rem(2) px2rem(6) px2rem(2) rgba(200, 200, 200, 0.3);
  }
  .shelf-item-comp {
    opacity: 1;
    &.is-edit {
      opacity: 0.5;
    }
  }
  .icon-selected {
    position: absolute;
    bottom: px2rem(2);
    right: px2rem(2);
    font-size: px2rem(18);
    color: rgba(0, 0, 0, 0.4);
    &.is-selected {
      color: $color-blue;
    }
  }
}
</style>
